package com.tuanzi.loan.business.service.family.impl;

import com.tuanzi.loan.business.entity.family.IcpWebComment;
import com.tuanzi.loan.business.repository.family.IcpWebCommentJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tuanzi.loan.core.aspect.Debug;

import com.tuanzi.loan.business.service.family.IcpWebCommentService;
import com.tuanzi.loan.vo.request.family.IcpWebCommentSearchRequest;
import com.tuanzi.loan.vo.response.family.IcpWebCommentSearchResponse;
import com.tuanzi.loan.business.repository.family.IcpWebCommentRepository;
import com.tuanzi.loan.core.vo.PageSearcher;
import org.springframework.data.domain.Page;
import java.util.Objects;

/**
 * ICP备案网站评论 ServiceImplement
 * @author JinRuiyang 2017/12/13 12:07:19
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class IcpWebCommentServiceImpl implements IcpWebCommentService {

    @Autowired
    private IcpWebCommentRepository icpWebcommentRepository;

    @Autowired
    private IcpWebCommentJpaRepository icpWebCommentJpaRepository;

    /**
     * 更新评论的发布状态
     *
     * @param id     评论id
     * @param status 状态值
     */
    @Override
    public void updatePublishStatus(Long id, Integer status) {
        if (Objects.isNull(id) || Objects.isNull(status))
            throw new IllegalArgumentException("参数 id 或 status 为空");
        IcpWebComment comment = icpWebCommentJpaRepository.findOne(id);
        if (Objects.isNull(comment))
            throw new IllegalStateException("找不到 id 对应的评论信息");
        if (comment.getIsEnable() == Disabled || comment.getStatus() == Published)
            throw new IllegalStateException("该评论无法进行发布操作");
        comment.setStatus(status);
        icpWebCommentJpaRepository.save(comment);
    }

    /**
     * 更新评论的可用状态
     *
     * @param id     评论id
     * @param status 状态值
     */
    @Override
    public void updateEnableStatus(Long id, Integer status) {
        if (Objects.isNull(id) || Objects.isNull(status))
            throw new IllegalArgumentException("参数 id 或 status 为空");
        IcpWebComment comment = icpWebCommentJpaRepository.findOne(id);
        if (Objects.isNull(comment))
            throw new IllegalStateException("找不到 id 对应的评论信息");
        if (comment.getIsEnable() == Disabled || comment.getStatus() == UnPublish)
            throw new IllegalStateException("该评论无法进行删除操作");
        comment.setIsEnable(status);
        icpWebCommentJpaRepository.save(comment);
    }

    /**
     * ICPWebComment查询
     * @param pageSearcher 请求信息
     * @return 响应列表
     */
    public Page<IcpWebCommentSearchResponse> list(PageSearcher<IcpWebCommentSearchRequest> pageSearcher) {
        if (Objects.isNull(pageSearcher))
            throw new NullPointerException("pageSearcher is null");
        if (Objects.isNull(pageSearcher.getSearchCondition()) || Objects.isNull(pageSearcher.getPageable()))
            throw new NullPointerException("icpWebCommentSearchRequest or pageable is null");
        return icpWebcommentRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }
}